<template>
  <div style="min-width: 809px">
    <div v-if="$route.path == '/invoiceList'">
      <div class="vs-search-box">
        <el-form ref="form" :inline="true" :model="paging" label-width="120px">
          <el-form-item label="开票日期">
            <el-date-picker
              v-model="value1"
              @change="setDate"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
          </el-form-item>
          <!-- <el-form-item label="门店" style="margin-right: 30px">
            <el-select
              v-model="paging.store_id"
              placeholder="请选择"
              style="width: 200px"
            >
              <el-option
                v-for="item in list"
                :key="item.id"
                :label="item.title"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item> -->
          <el-form-item label="开票状态">
            <el-select clearable v-model="paging.status" placeholder="请选择">
              <el-option label="待开票" value="0"> </el-option>
              <el-option label="已开票" value="1"> </el-option>
              <el-option label="废票" value="2"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="接收方式">
            <el-select
              clearable
              v-model="paging.send_type"
              placeholder="请选择"
            >
              <el-option label="邮箱 " value="1"> </el-option>
              <el-option label="短信" value="2"> </el-option>
              <el-option label="邮寄" value="3"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="" class="vs-right-btns">
            <el-button @click="reset">重置</el-button>
            <el-button @click="handleCurrentChange(1)" type="primary"
              >查询</el-button
            >
          </el-form-item>
          <el-form-item label="购票方名称">
            <el-input type="text" v-model="paging.rise_name"></el-input>
          </el-form-item>
          <!-- <el-form-item label="订单编号">
            <el-input type="text" v-model="paging.username"></el-input>
          </el-form-item> -->
        </el-form>
      </div>
      <div class="vs-table-box">
        <el-table
          v-loading="loading"
          :data="tableData"
          header-row-class-name="has-gutter-table"
          style="width: 100%"
        >
          <el-table-column prop="out_trade_no" label="订单编号" min-width="100">
          </el-table-column>
          <el-table-column prop="rise_name" label="购票方名称" min-width="100">
            <!-- <template slot-scope="scope">
              <span v-if="scope.row.company">{{ scope.row.company.name }}</span>
            </template> -->
          </el-table-column>
          <el-table-column
            prop="identification"
            label="纳税人识别号"
            min-width="100"
          >
          </el-table-column>
          <el-table-column prop="money" label="金额（元）" min-width="100">
          </el-table-column>
          <el-table-column prop="" label="税率（%）" min-width="100">
            <template slot-scope="scope">
              <span v-if="scope.row.company">{{
                scope.row.company.tax_rate
              }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="ratePrice" label="税额（元）" min-width="100">
          </el-table-column>
          <el-table-column
            prop="totalPrice"
            label="价税合计金额（元）"
            min-width="130"
          >
          </el-table-column>
          <el-table-column prop="invoice_date" label="开票日期" min-width="100">
          </el-table-column>
          <el-table-column prop="invoice_code" label="发票代码" min-width="100">
          </el-table-column>
          <el-table-column prop="invoice_num" label="发票号码" min-width="100">
          </el-table-column>
          <el-table-column label="接收方式" min-width="100">
            <template slot-scope="scope">
              <span>{{ scope.row.send_type | send_type }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="create_time" label="状态" min-width="100">
            <template slot-scope="scope">
              <span>{{ scope.row.status | status }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="110">
            <template slot-scope="scope">
              <!-- <span
                v-if="scope.row.status == 1"
                @click="setRow(1, scope.row)"
                class="tab-table-text"
                >开票</span
              > -->
              <span @click="setRow(2, scope.row)" class="tab-table-text"
                >查看</span
              >
              <span
                v-if="scope.row.status == 1"
                @click="setRow(3, scope.row)"
                class="tab-red-text"
                >作废</span
              >
              <!-- <div v-if="scope.row.invoiceSerialNum">
                <span
                  class="tab-table-text"
                  v-if="
                    (scope.row.status < 22 && scope.row.status != 3) ||
                    (scope.row.status < 22 && scope.row.status != 2)
                  "
                  @click="setRow(4, scope.row)"
                  >更新状态</span
                >
              </div> -->
            </template>
          </el-table-column>
        </el-table>
        <div class="vs-paging-box">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="paging.page"
            :page-size="paging.limit"
            layout="sizes,prev, pager, next, jumper"
            :total="paging.total"
          >
          </el-pagination>
        </div>
      </div>
    </div>
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  data() {
    return {
      value1: "",
      loading: false,
      loading2: false,
      isBtn: false,
      isInfo: false,
      paging: {
        rise_name: "",
        start: "",
        end: "",
        status: "",
        send_type: "",
        page: 1,
        total: 0,
        limit: 10,
      },
      list: [],
      form: {
        date: "",
      },
      tableData: [],
    };
  },
  created() {
    this.repairGetList();
  },
  filters: {
    send_type(val) {
      switch (val) {
        case 1:
          val = "邮箱";
          break;
        case 2:
          val = "短信";
          break;
        case 3:
          val = "邮寄";
          break;
      }
      return val;
    },
    status(val) {
      switch (val) {
        case 1:
          val = "待开票";
          break;
        case 2:
          val = "已开票";
          break;
        case 3:
          val = "发票已作废";
          break;
        case 20:
          val = "开票中";
          break;
        case 21:
          val = "开票成功签章中";
          break;
        case 22:
          val = "开票失败";
          break;
        case 24:
          val = "开票成功签章失败";
          break;
        case 31:
          val = "发票作废中";
          break;
      }
      return val;
    },
  },
  methods: {
    setDate(e) {
      this.paging.start = e[0];
      this.paging.end = e[1];
    },
    async setRow(type, row) {
      if (type == 1) {
        this.utils.goUrl(`/invoiceDetails?type=${type}&id=${row.id}`);
      } else if (type == 2) {
        this.utils.goUrl(`/invoiceDetails?type=${type}&id=${row.id}`);
      } else if (type == 4) {
        //更新用户状态
        let { code, data } = await this.$Http.queryInvoiceResult({
          id: row.id,
        });
        if (code) {
          this.handleCurrentChange(1);
          this.$message.success("更新成功");
        }
      } else {
        //作废
        this.$msgbox({
          title: "提示",
          message: "您确定要作废此发票?",
          type: "warning",
          showCancelButton: true,
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          beforeClose: async (action, instance, done) => {
            if (action === "confirm") {
              instance.confirmButtonLoading = true;
              instance.confirmButtonText = "执行中...";
              let { code, data } = await this.$Http.setStatus({ id: row.id });
              if (code) {
                this.$message.success("作废成功!");
                this.handleCurrentChange(1);
              }
              done();
              instance.confirmButtonLoading = false;
            } else {
              done();
            }
          },
        })
          .then((action) => {})
          .catch(() => {});
      }
    },
    async repairGetList() {
      this.loading = true;
      let { code, data } = await this.$Http.getInvoice(this.paging);
      this.loading = false;
      if (code) {
        data.data.forEach((ele) => {
          // ele.company.tax_rate = ele.company.tax_rate
          let price = ele.money * 1 * (ele.company.tax_rate / 100).toFixed(2);
          ele.tax_rate_money = price;
          ele.totalPrice = ele.money * 1 + ele.tax_rate_money;
        });
        this.tableData = data.data;
        this.paging.limit = data.per_page;
        this.paging.total = data.total;
      }
    },
    handleSizeChange(val) {
      this.paging.limit = val;
      this.repairGetList();
    },
    handleCurrentChange(val) {
      this.paging.page = val;
      this.repairGetList();
    },
    reset() {
      //重置
      this.paging = this.$options.data().paging;
      // this.form.date = "";
      this.handleCurrentChange(1);
    },
  },
};
</script>

<style lang="scss" scoped>
.image-thumbnail {
  width: 130px;
  height: 130px;
}
</style>